package com.hk.common.util;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @Author xuebinlu
 * @Date 2018/6/27 9:37
 * @Version 1.0
 * @Description 图片工具类
 **/
@Slf4j
public class Images {

    /**
     * 将一个图片文件读入内存
     *
     * @param img
     *            图片文件
     * @return 图片对象
     */
    public static BufferedImage read(Object img) {
        try {
            if (img instanceof BufferedImage) {
                return (BufferedImage) img;
            }
            if (img instanceof File)
                return ImageIO.read((File) img);

            if (img instanceof URL)
                img = ((URL) img).openStream();
            log.error(new Exception("Unkown img info!! --> " + img).toString());
        } catch (IOException e) {
            e.fillInStackTrace();
        }
        return null;
    }


}
